#ifndef __JUDGE_POTOCOL_H
#define __JUDGE_POTOCOL_H

#include "main.h"
#include "crc.h"
#include <stdbool.h>

// 机器人间交互数据段长度
#define INTERACT_DATA_LEN	10
extern uint8_t jbus_rx_buffer[200];
extern void judge_update(void); // 裁判系统接收信息

typedef enum {  //设备工作状态
	DEV_ONLINE,
	DEV_OFFLINE,
} dev_work_state_t;

typedef enum {  // 错误代码
	NONE_ERR,		  // 正常(无错误)
	DEV_ID_ERR,		// 设备ID错误
	DEV_INIT_ERR,	// 设备初始化错误
	DEV_DATA_ERR,	// 设备数据错误
} dev_errno_t;

typedef enum {  // 设备id列表
//	DEV_ID_RC = 0,
//	DEV_ID_IMU = 1,
//	DEV_ID_CHASSIS = 2,
//	DEV_ID_DIAL = 3,
//	DEV_ID_GIMBAL_PITCH = 4,
//	DEV_ID_GIMBAL_YAW = 5,
//	DEV_ID_ENCODER_AND_TOUCH = 6,
//	DEV_ID_VISION = 7,
	DEV_ID_JUDJE = 8,
//	DEV_ID_CNT = 9,
} dev_id_t;

/* 自定义帧头 Byte: 5 */
typedef __packed struct
{
    uint8_t  sof;
    uint16_t data_length;
    uint8_t  seq;
    uint8_t  crc8;
} std_frame_header_t;




/* ID: 0x0001	Byte: 	11	比赛状态数据 */
typedef __packed struct
{
    uint8_t game_type : 4;			  // 比赛类型
    uint8_t game_progress : 4;		// 比赛阶段
    uint16_t stage_remain_time;		// 当前阶段剩余时间(单位:s)
    uint64_t SyncTimeStamp;       //机器人接收到该指令的精确 Unix 时间，当机载端收到有效的 NTP 服务器授时后生效
} ext_game_status_t;

/* ID: 0x0002	Byte:	1	比赛结果数据 */
typedef __packed struct
{
    uint8_t winner;
} ext_game_result_t;

/* ID: 0x0003	Byte:	32	机器人血量数据数据 */
typedef __packed struct
{
    uint16_t red_1_robot_HP;	// 红1英雄机器人血量(未上场及罚下血量为0)
    uint16_t red_2_robot_HP;	// 红2工程机器人血量
    uint16_t red_3_robot_HP;	// 红3步兵机器人血量
    uint16_t red_4_robot_HP;	// 红4步兵机器人血量
    uint16_t red_5_robot_HP;	// 红5步兵机器人血量
    uint16_t red_7_robot_HP;	// 红7哨兵机器人血量
    uint16_t red_outpost_HP;	// 红方前哨站血量
    uint16_t red_base_HP;		  // 红方基地血量
    uint16_t blue_1_robot_HP;	// 蓝1英雄机器人血量
    uint16_t blue_2_robot_HP;	// 蓝2工程机器人血量
    uint16_t blue_3_robot_HP;	// 蓝3步兵机器人血量
    uint16_t blue_4_robot_HP;	// 蓝4步兵机器人血量
    uint16_t blue_5_robot_HP;	// 蓝5步兵机器人血量
    uint16_t blue_7_robot_HP;	// 蓝7哨兵机器人血量
    uint16_t blue_outpost_HP;	// 蓝方前哨站血量
    uint16_t blue_base_HP;		// 蓝方基地血量
} ext_game_robot_HP_t;

/* ID: 0x0105 	Byte:	3	飞镖发射状态 */
typedef __packed struct
{
    uint8_t dart_belong;
    uint16_t stage_remaining_time;
} ext_dart_status_t;

/* ID: 0x0101  Byte:  4    场地事件数据 */
typedef __packed struct
{
    uint32_t event_data; 
//	uint32_t event_type;
} ext_event_data_t;


/* ID: 0x0102  Byte:  4    补给站动作标识数据 */
typedef __packed struct
{
    uint8_t supply_projectile_id;
    uint8_t supply_robot_id;
    uint8_t supply_projectile_step;
    uint8_t supply_projectile_num;
} ext_supply_projectile_action_t;

/* ID: 0X0104  Byte:  3	   裁判警告信息 */
typedef __packed struct
{
    uint8_t level;
    uint8_t foul_robot_id;
	uint8_t count;
} ext_referee_warning_t;

/* ID: 0X0201  Byte: 13    机器人状态数据 */
typedef __packed struct
{
    uint8_t robot_id;
    uint8_t robot_level;
    uint16_t remain_HP;
    uint16_t max_HP;
	uint16_t shooter_barrel_cooling_value; 
	uint16_t shooter_barrel_heat_limit; 
    uint16_t chassis_power_limit;
    uint8_t mains_power_gimbal_output : 1;
    uint8_t mains_power_chassis_output : 1;
    uint8_t mains_power_shooter_output : 1;
} ext_game_robot_status_t;


/* ID: 0X0202  Byte: 16    实时功率热量数据 */
typedef __packed struct
{
    uint16_t chassis_volt;   		// 底盘输出电压，单位：mV
    uint16_t chassis_current;		// 底盘输出电流，单位：mA
    float chassis_power;   			// 瞬时功率，单位：W
    uint16_t chassis_power_buffer;	// 底盘功率缓冲
    uint16_t  shooter_id1_17mm_cooling_heat;		// 17mm 枪口热量
    uint16_t shooter_id2_17mm_cooling_heat;	// 机动17mm 枪口热量
    uint16_t shooter_id1_42mm_cooling_heat;	//  42mm 枪口热量
} ext_power_heat_data_t;


/* ID: 0x0203  Byte: 12    机器人位置数据 */
typedef __packed struct
{
    float x;
    float y;
    float angle;
} ext_game_robot_pos_t;


/* ID: 0x0204  Byte:  6    机器人增益数据 */
typedef __packed struct
{
  uint8_t recovery_buff;  
  uint8_t cooling_buff;  
  uint8_t defence_buff;  
  uint8_t vulnerability_buff; 
  uint16_t attack_buff; 
} ext_buff_t;


/* ID: 0x0205  Byte:  2    空中机器人状态数据 */
typedef __packed struct
{
   uint8_t airforce_status; 
   uint8_t time_remain; 
} ext_aerial_robot_energy_t;


/* ID: 0x0206  Byte:  1    伤害状态数据 */
typedef __packed struct
{
    uint8_t armor_id : 4; 	// 装甲伤害时代表装甲ID
    uint8_t hurt_type : 4; 	// 0x0装甲伤害 0x1模块掉线 0x2超射速 0x3超热量 0x4超功率 0x5撞击
} ext_robot_hurt_t;


/* ID: 0x0207  Byte:  7    实时射击数据 */
typedef __packed struct
{
    uint8_t bullet_type; 	// 子弹类型(1-17mm, 2-42mm)
    uint8_t shooter_id;
    uint8_t bullet_freq;  	// 子弹射频(Hz)
    float bullet_speed;		// 子弹射速(m/s)
} ext_shoot_data_t;


/* ID: 0x0208  Byte:  6    允许发弹量 */
typedef __packed struct
{
  uint16_t projectile_allowance_17mm; 
  uint16_t projectile_allowance_42mm;  
  uint16_t remaining_gold_coin; 
} ext_bullet_remaining_t;


/* ID: 0x0209  Byte:  4 	机器人RFID状态 */
typedef __packed struct
{
    uint32_t rfid_status;
} ext_rfid_status_t;

/*ID: 0X20A	Byte:	6	飞镖选手端指令数据*/
typedef __packed struct 
{
  uint8_t dart_launch_opening_status;  
  uint8_t reserved;  
  uint16_t target_change_time;  
  uint16_t latest_launch_cmd_time; 
} ext_dart_client_cmd_t; 

/*ID:	0x20B	Byte:	40*/
typedef __packed struct 
{ 
  float hero_x;  
  float hero_y;  
  float engineer_x;  
  float engineer_y;  
  float standard_3_x;  
  float standard_3_y;  
  float standard_4_x;  
  float standard_4_y;  
  float standard_5_x;  
  float standard_5_y; 
}ext_ground_robot_position_t; 

/*ID:	0x20C	Byte:	6*/
typedef __packed struct 
{ 
  uint8_t mark_hero_progress;  
  uint8_t mark_engineer_progress;  
  uint8_t mark_standard_3_progress;  
  uint8_t mark_standard_4_progress; 
  uint8_t mark_standard_5_progress; 
  uint8_t mark_sentry_progress; 
}ext_radar_mark_data_t; 

/*ID:	0x20D	Byte:	4*/
typedef __packed struct 
{ 
  uint32_t sentry_info; 
} ext_sentry_info_t; 

/*ID:	0x20E	Byte:	1*/
typedef __packed struct 
{ 
  uint8_t radar_info; 
} ext_radar_info_t; 

/* ID: 0x301  Byte:  128 	云台手交互 */
typedef __packed struct
{
    uint16_t cmd_id;
    uint16_t send_id;
    uint16_t receive_id;
	uint8_t user_data[113]; 
//    int8_t control_dir; //-1和1
} ext_aerial_data_t;

/*ID: 0x302  Byte:  30*/
typedef __packed struct 
{ 
uint8_t data[30]; 
}ext_custom_robot_data_t; 

/* ID: 0x0303  Byte:  15 	 */
typedef __packed struct
{
    float target_x;
    float target_y;
    uint8_t commd_keyboard;
    uint8_t target_ID;
	uint8_t cmd_source;
} ext_robot_command_t;

/* ID: 0x0304  Byte:  12 	 */
typedef __packed struct 
{ 
	int16_t mouse_x; 
	int16_t mouse_y; 
	int16_t mouse_z; 
	int8_t left_button_down; 
	int8_t right_button_down; 
	uint16_t keyboard_value; 
	uint16_t reserved; 
}ext_remote_control_t;
/* ID: 0x0305  Byte:  10 	 */
typedef __packed struct 
{ 
uint16_t target_robot_id; 
float target_position_x; 
float target_position_y; 
}ext_map_robot_data_t;

/*ID: 0x0306    Byte:  8 */
typedef __packed struct 
{ 
uint16_t key_value; 
  uint16_t x_position:12; 
  uint16_t mouse_left:4; 
  uint16_t y_position:12; 
  uint16_t mouse_right:4; 
  uint16_t reserved; 
}ext_custom_client_data_t; 
/* ID: 0x0307  Byte:  105 	 */
typedef __packed struct 
{ 
uint8_t intention; 
uint16_t start_position_x; 
uint16_t start_position_y; 
int8_t delta_x[49]; 
int8_t delta_y[49]; 
uint16_t sender_id; 
}ext_map_data_t; 

/* ID: 0x0308  Byte:  34 	 */
typedef __packed struct 
{  
uint16_t sender_id; 
uint16_t receiver_id; 
uint8_t user_data[30]; 
}ext_custom_info_t; 

typedef __packed struct
{
	std_frame_header_t frame_header;
	ext_custom_robot_data_t     CustomRobotData;
	ext_remote_control_t        RemoteControlCommand;			//0x0304
	uint32_t frame_length;

}TUC_typedef;

/* Exported types 2 ------------------------------------------------------------*/
/* 裁判信息包 */
typedef struct
{
    uint16_t frame_length;					// 整帧长度(调试时使用)
    uint16_t cmd_id;		    				// 命令码(调试时使用)
    uint16_t err_cnt;		    				// 错帧数(调试时使用)
    bool	   data_valid;	    			// 数据有效性
    uint16_t self_client_id;				// 发送者机器人对应的客户端ID
    bool	power_heat_update;				// 功率热量数据更新
    bool	shoot_update;		  				// 射击数据更新
    bool	hurt_data_update;				  // 伤害数据更新
    bool	dart_data_update;	 				// 飞镖数据更新
    bool	supply_data_update;	      // 补给站数据更新
    bool    command_data_update;      //小地图数据更新
    bool	communication_data_update;//云台手数据更新
	
	std_frame_header_t				FrameHeader;				// 帧头信息
    ext_game_status_t 				GameStatus;					// 0x0001
    ext_game_result_t 				GameResult;					// 0x0002
    ext_game_robot_HP_t 			GameRobotHP;		 		// 0x0003

	ext_event_data_t					EventData;					// 0x0101
    ext_supply_projectile_action_t	SupplyProjectileAction;		// 0x0102
    ext_referee_warning_t			RefereeWarning;			// 0x0104
    ext_dart_status_t				  DartStatus;					// 0x0105
    ext_game_robot_status_t		GameRobotStatus;		// 0x0201
    ext_power_heat_data_t			PowerHeatData;			// 0x0202
    ext_game_robot_pos_t			GameRobotPos;				// 0x0203
    ext_buff_t								Buff;								// 0x0204
    ext_aerial_robot_energy_t	AerialRobotEnergy;	// 0x0205
    ext_robot_hurt_t					RobotHurt;					// 0x0206
    ext_shoot_data_t					ShootData;					// 0x0207
    ext_bullet_remaining_t		BulletRemaining;		// 0x0208
    ext_rfid_status_t					RfidStatus;					// 0x0209
	ext_dart_client_cmd_t		DataClientcmd;		//0x020A
	ext_ground_robot_position_t		GroundRobotPos;		//0x020B
	ext_radar_mark_data_t		RadarMarkData;		//0x020C
	ext_sentry_info_t			SentryInfo;		//0x020D
	ext_radar_info_t			RadarInfo;		//0x020E
    ext_aerial_data_t         AerialData;         // 0x301
    ext_custom_robot_data_t     CustomRobotData;     // 0x302
    ext_robot_command_t       RobotCommand;			//0x0303
    ext_remote_control_t        RemoteControlCommand;			//0x0304
	ext_map_robot_data_t		MapRobotData;		//0x0305
    ext_custom_client_data_t    CustomClientData;     // 0x306
	ext_map_data_t			MapData;		//0x0307
	ext_custom_info_t		CustomInfo;		//0x0308
	
	int16_t		offline_cnt;
    int16_t		offline_max_cnt;
		
}__attribute__((packed))judge_info_t;

typedef struct judge_sensor_struct
{
    judge_info_t*	info;
    void	(*init)(struct judge_sensor_struct* self);
    void	(*update)(void);
    void	(*check)(struct judge_sensor_struct* self);
    void	(*heart_beat)(struct judge_sensor_struct* self);
    dev_work_state_t	work_state;
    dev_errno_t			errno;
    dev_id_t			id;
} judge_sensor_t;

extern judge_sensor_t	judge_sensor;
extern void judge_init(judge_sensor_t* judge);
void Judge_Init_DMA(void);

#endif

